package com.dup.jxlh.admin.controller;

import com.dup.jxlh.admin.entities.auth.AuthUser;
import com.dup.jxlh.api.entities.CommonResult;
import com.dup.jxlh.api.enums.ResultEnum;
import com.dup.jxlh.api.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Map;

@RestController
@RequestMapping("/oauth")
public class Oauth2Controller {
    @Autowired
    private TokenEndpoint tokenEndpoint;

    @PostMapping("/token")
    public CommonResult postAccessTokenWithUserInfo(Principal principal, @RequestParam Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
        OAuth2AccessToken accessToken = tokenEndpoint.postAccessToken(principal, parameters).getBody();
        return ResultUtil.result(ResultEnum.SUCCESS, accessToken);
    }

    @PostMapping("/userInfo")
    public CommonResult getUserInfo(@AuthenticationPrincipal AuthUser authUser) {
        return ResultUtil.result(ResultEnum.SUCCESS, authUser);
    }
}
